import router from '..'
import Route from '../entity/Route'
import activityRouteModules from './activity'
import dishRouteModules from './dish'
import basicInfoModule from './basic-info'
import businessConfigModule from './business-config'
// 所有的路由模块
const allModules = [activityRouteModules, dishRouteModules, basicInfoModule, businessConfigModule]
// 导出让route->DynamicRouting来主动注册（因为两者注册都是在同一个地方，所以就一起了，只不过allModulesRoutes是静态的）
export const allModulesRoutes = allModules.reduce((allRoute, module) => {
  allRoute.push(...(Array.isArray(module.routeList) ? module.routeList : Object.values(module.routeList)))
  return allRoute
}, [] as Route[])
// 导出导航，仅需要在对应的页面调用moduleRouteNavigation再输入key即可
export function moduleRouteNavigation(key: string | number, urlParams: { [key: string]: any } = {}) {
  // 获取当前路由信息
  const currentPath = router.currentRoute.value.path
  for (const module of allModules) {
    if (!Array.isArray(module.routeList) && module.parentPath === currentPath) {
      if (module.routeList[key] == null) throw Error('路由未定义！')
      router.push({ path: module.routeList[key].path, query: urlParams })
      return
    }
  }
  throw new Error('子路由没有找到！')
}
